package com.clwl.supervise.superviseapp.util.baiduAPI;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clwl.supervise.superviseapp.entity.scan.SIllegalAdditives;
import com.clwl.supervise.superviseapp.entity.scan.TjTjjbz;
import com.clwl.supervise.superviseapp.mapper.scan.SIllegalAdditivesMapper;
import com.clwl.supervise.superviseapp.mapper.scan.TjTjjbzMapper;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 扫一扫公共内容
 */
@Component
public class ScanUtil {


    private static SIllegalAdditivesMapper sIllegalAdditivesMapper;


    private static TjTjjbzMapper tjTjjbzMapper;


    public static Map<String, Object> scanPhotoTianjin(String audioUrl, String type, String foodType) throws Exception {

        //上传图片
        Map<String, Object> re = new HashMap<>();
        switch (type) {
            case "2":
                //识别文字
                org.json.JSONArray resultJSONArray = BaiduAipUtil.textSpot(audioUrl);
                BaiduAipUtil.deleteSpotImage(audioUrl);
                if (resultJSONArray != null) {
                    StringBuffer s = new StringBuffer();
                    for (int i = 0; i < resultJSONArray.length(); i++) {
                        s.append(resultJSONArray.getJSONObject(i).getString("words"));
                    }
                    re.put("text", s.toString());
                    re.put("code", "0");
                } else {
                    re.put("code", "1");
                }
                break;
            case "3":
                //物品识别
                org.json.JSONArray jsonArray1 = BaiduAipUtil.materialSpot(audioUrl);
                BaiduAipUtil.deleteSpotImage(audioUrl);
                if (jsonArray1 != null) {
                    StringBuffer s = new StringBuffer();
                    for (int i = 0; i < jsonArray1.length(); i++) {
                        s.append("结果");
                        s.append(i + 1);
//                            s.append(jsonArray1.getJSONObject(i).getString("root"));
                        s.append("：");
                        s.append(jsonArray1.getJSONObject(i).getString("keyword"));
                        if (jsonArray1.getJSONObject(i).has("baike_info")) {
                            s.append("\n");
                            s.append(jsonArray1.getJSONObject(i).getJSONObject("baike_info").getString("description"));
                        }
                        s.append("\n");
                    }
                    re.put("text", s.toString());
                    re.put("code", "0");
                } else {
                    re.put("code", "1");
                }
                break;
            case "4":
                //条码二维码识别
                org.json.JSONArray resultJSONArray2 = BaiduAipUtil.codeSpot(audioUrl);
                BaiduAipUtil.deleteSpotImage(audioUrl);
                if (resultJSONArray2 != null) {
                    StringBuffer s = new StringBuffer();
                    for (int i = 0; i < resultJSONArray2.length(); i++) {
                        s.append(resultJSONArray2.getJSONObject(i).getJSONArray("text").get(0));
                    }
                    re.put("text", s.toString());
                    re.put("code", "0");
                } else {
                    re.put("code", "1");
                }
                break;
            case "5":
                //食品添加剂识别
                org.json.JSONArray resultJSONArray4 = BaiduAipUtil.textSpot(audioUrl);
                BaiduAipUtil.deleteSpotImage(audioUrl);
                if (resultJSONArray4 != null) {
                    StringBuffer s = new StringBuffer();
                    for (int i = 0; i < resultJSONArray4.length(); i++) {
                        JSONObject jsonObject = resultJSONArray4.getJSONObject(i);
                        String content = jsonObject.getString("words");
                        //去掉配料等信息
                        content = reloaceText(content);
                        s.append(content);
                    }
                    List<String> contentList = new ArrayList<>();
                    if (s.indexOf("、") >= 0) {
                        String[] contentArray = s.toString().split("、");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else if (s.indexOf("，") >= 0) {
                        String[] contentArray = s.toString().split("，");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else if (s.indexOf(",") >= 0) {
                        String[] contentArray = s.toString().split(",");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else if (s.indexOf("(") >= 0) {
                        String[] contentArray = s.toString().split("\\(");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else if (s.indexOf(")") >= 0) {
                        String[] contentArray = s.toString().split("\\)");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else if (s.indexOf("(") >= 0) {
                        String[] contentArray = s.toString().split("\\（");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else if (s.indexOf(")") >= 0) {
                        String[] contentArray = s.toString().split("\\）");
                        contentList.addAll(Arrays.asList(contentArray));
                    } else {
                        String[] contentArray = s.toString().split(" ");
                        contentList.addAll(Arrays.asList(contentArray));
                    }
                  //  List<Map<String, Object>> rList = checkSillegalAdditivesTianjin(contentList, foodType);
                    //获取用料标准
                    //String[] foodTypeArr = foodType.split(",");
                   // List<TjTjjbz> tjTjjbz = tjTjjbzMapper.selectFtCode(foodTypeArr[foodTypeArr.length - 1]);
                    //文件上传

                    re.put("text", contentList);
//                    re.put("baList", tjTjjbz);
                    re.put("code", "0");
                } else {
                    re.put("code", "1");
                }
                break;
        }
        return re;
    }

    /**
     * 识别添加剂是否是非法
     *
     * @param contentList
     * @return
     */
    private static List<Map<String, Object>> checkSillegalAdditivesTianjin(List<String> contentList, String foodType) {
        List<Map<String, Object>> rList = new ArrayList<>();
        // TjFoodtype foodTypeEntity =  tjFoodtypeMapper.selectByCode(foodType);
        String[] foodTypeArr = foodType.split(",");
        List<TjTjjbz> tjTjjbzList = tjTjjbzMapper.selectByCode(foodTypeArr[foodTypeArr.length - 1]);

        QueryWrapper<SIllegalAdditives> sIllegalAdditivesQueryWrapper = new QueryWrapper<>();
        sIllegalAdditivesQueryWrapper.eq("delete_flag", "0");
        List<SIllegalAdditives> sIllegalAdditivesList = sIllegalAdditivesMapper.selectList(sIllegalAdditivesQueryWrapper);
        for (String content : contentList) {
            Map<String, Object> rMap = new HashMap<>();
            String name = reloaceText(content);
            rMap.put("text", name);
            for (SIllegalAdditives sIllegalAdditives : sIllegalAdditivesList) {
                if (sIllegalAdditives.getAdditivesName().equals(content)) {
                    //是违法添加剂
                    rMap.put("code", 0);
                    break;
                } else {
                    rMap.put("code", 1);
                }
            }
            ;
            rMap.put("color", "#bf5100");
            for (TjTjjbz tjTjjbz : tjTjjbzList) {
                if (tjTjjbz != null && tjTjjbz.getName() != null && StringUtils.equals(name.trim(), tjTjjbz.getName().trim())) {
                    rMap.put("dosage", tjTjjbz.getUseMax() + (tjTjjbz.getUnit() == null ? "" : tjTjjbz.getUnit()));
                    rMap.put("color", "green");
                }
            }
            rList.add(rMap);
        }
        return rList;
    }

    /**
     * 识别添加剂是否是非法
     *
     * @param contentList
     * @return
     */
    private static List<Map<String, Object>> checkSillegalAdditives(List<String> contentList) {
        List<Map<String, Object>> rList = new ArrayList<>();
        QueryWrapper<SIllegalAdditives> sIllegalAdditivesQueryWrapper = new QueryWrapper<>();
        sIllegalAdditivesQueryWrapper.eq("delete_flag", "0");
        List<SIllegalAdditives> sIllegalAdditivesList = sIllegalAdditivesMapper.selectList(sIllegalAdditivesQueryWrapper);
        for (String content : contentList) {
            Map<String, Object> rMap = new HashMap<>();
            rMap.put("text", reloaceText(content));
            for (SIllegalAdditives sIllegalAdditives : sIllegalAdditivesList) {
                if (sIllegalAdditives.getAdditivesName().equals(content)) {
                    //是违法添加剂
                    rMap.put("code", 0);
                    break;
                } else {
                    rMap.put("code", 1);
                }
            }
            ;
            rList.add(rMap);
        }
        return rList;
    }


    private static String reloaceText(String content) {
        content = content.replaceAll("【", "").replaceAll("】", "").replaceAll("原料表", "").
                replaceAll("配料表", "").replaceAll("配料", "").replaceAll("原料", "").
                replaceAll(":", "").replaceAll("：", "").replaceAll("原  料", "");
        return content;
    }


    public SIllegalAdditivesMapper getsIllegalAdditivesMapper() {
        return ScanUtil.sIllegalAdditivesMapper;
    }

    @Resource
    public void setsIllegalAdditivesMapper(SIllegalAdditivesMapper sIllegalAdditivesMapper) {
        ScanUtil.sIllegalAdditivesMapper = sIllegalAdditivesMapper;
    }

    @Resource
    public void setTjTjjbzMapper(TjTjjbzMapper tjTjjbzMapper) {
        ScanUtil.tjTjjbzMapper = tjTjjbzMapper;
    }
}
